package com.deng.springboot.demo.web.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 邓力宾
 * @date 2020/3/1111:45
 * @Description: 自定义spring 的异步方法调用的任务执行器 执行有@Async注解的方法(如果用户自己没有定义异步任务执行器，容器使用默认的)
 */
@Component
public class SpringAsyncTaskExecutorImp implements TaskExecutor {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private ThreadPoolExecutor executor;

    /**
     * 注入自己配置的线程池
     * @param executor
     */
    @Autowired
    public void setExecutor(ThreadPoolExecutor executor){
        this.executor = executor;
    }

    /**
     * 重写该方法 执行异步任务
     * @param runnable
     */
    @Override
    public void execute(Runnable runnable) {
        logger.info("执行异步任务=>:");
        executor.execute(runnable);
    }
}
